Skip to content

Add emit_result_slice_pointer option for :many queries#11

Open
Alexfilus wants to merge 1 commit intosqlc-dev:mainfrom
Alexfilus:emit-result-slice-pointer-clean
Open

Add emit_result_slice_pointer option for :many queries#11
Alexfilus wants to merge 1 commit intosqlc-dev:mainfrom
Alexfilus:emit-result-slice-pointer-clean

Conversation

@Alexfilus
Copy link

Summary

  • Adds new emit_result_slice_pointer option that changes :many query signature to accept a destination slice pointer instead of returning a new slice
  • When enabled, generated functions use func(..., dest *[]T) error instead of func(...) ([]T, error)
  • This optimization eliminates allocations by allowing callers to reuse pre-allocated slices

Example

Default behavior (emit_result_slice_pointer: false):

func (q *Queries) GetItems(ctx context.Context, ids []string) ([]Item, error)

With emit_result_slice_pointer: true:

func (q *Queries) GetItems(ctx context.Context, ids []string, dest *[]Item) error

Changes

  • internal/opts/options.go: Added EmitResultSlicePointer option
  • internal/gen.go: Wired option to template context
  • internal/templates/pgx/queryCode.tmpl: Added conditional template for new behavior

Test plan

  • Built with make all
  • Ran make test - all tests pass
  • Tested locally with real sqlc project

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant